//
//  ZSLoginViewController.m
//  ZhuShou
//
//  Created by Dejun Liu on 15/10/29.
//  Copyright © 2015年 jw. All rights reserved.
//

#import "ZSLoginViewController.h"
#import "ZSRegistViewController.h"
#import "ZSForgetPasswordViewController.h"
#import "ZSAccountHistoriesTableViewController.h"

@interface ZSLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIImageView *accountIconImageView;
@property (weak, nonatomic) IBOutlet UIImageView *passwordIconImageView;

@end

@implementation ZSLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
}

- (void)initViews
{
    [super initViews];
    
    //输入历史
    ZSAccountHistoriesTableViewController *vc = [[ZSAccountHistoriesTableViewController alloc] initWithStyle:UITableViewStylePlain];
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];
    vc.view.top = self.accountTextField.bottom;
    vc.view.width = self.accountTextField.right - self.accountIconImageView.left;
    vc.view.left = self.accountIconImageView.left;
    vc.view.height = 160.0f;
    vc.view.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender {

    UIViewController *controller = self.childViewControllers.lastObject;
    controller.view.hidden = YES;
    
    [self br_progressHUD:YES];
    
//    @weakify(self);
//    [self performAfter:2 block:^{
//        @strongify(self)
//        [self br_dismissViewController];
//    }];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params hs_setSafeValue:@"email" forKey:@"type"];
    [params hs_setSafeValue:self.accountTextField.text forKey:@"name"];
    [params hs_setSafeValue:self.passwordTextField.text forKey:@"value"];
    
    [self br_progressHUD:YES];
    @weakify(self);
    [BRServiceCenter BRServiceCenterUPDWithAction:@"login" CMD:[BRCommand CommandLogin] params:params response:^(BRMessageResponseType type, NSInteger retryCount, id responseObj) {
        if (responseObj) {
            @strongify(self);
            [self br_progressHUD:NO];
            BRUDPResponse *response = responseObj;
            if ([response.state isEqualToString:@"0000"] || response) {
                [self br_alertViewWithTitle:@"提示" content:response.stateDesc operationIndex:^(NSInteger clickedIndex) {
                    [self br_dismissViewController];
                }];
            }else
            {
                [self br_AutoDisappearWithText:response.stateDesc];
            }
        }
    }];
}

- (IBAction)rememberPassword:(id)sender {
}

- (IBAction)autoLogin:(id)sender {
    
}

- (IBAction)registMember:(id)sender {
    [self br_pushViewControllerWithClassName:@"ZSRegistViewController"];
}

- (IBAction)forgetPassword:(id)sender {
    [self br_pushViewControllerWithClassName:@"ZSForgetPasswordViewController"];
}

- (IBAction)moreAccount:(id)sender {
    UIViewController *controller = self.childViewControllers.lastObject;
    if (controller.view.hidden) {
        controller.view.hidden = NO;
    }else
    {
        controller.view.hidden = YES;
    }
}

@end
